package mk.com.ldm.corepoint.implementation.client.lib.wrappers.widgets;

import org.gwt.mosaic.ui.client.InfoPanel;

import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseWidget;
import mk.com.ldm.corepoint.implementation.client.lib.validation.Validator;
import mk.com.ldm.corepoint.implementation.client.presenter.EventHandler;
import mk.com.ldm.corepoint.implementation.shared.FormRow;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class TextBoxWrapper extends BaseWidget{
	
	private TextBox textbox = new TextBox();
	private Label label = new Label();
	private FormRow config;
	
	public TextBoxWrapper(final FormRow config, HandlerManager eventBus) {
		super(config);
		
		this.config = config;
		
		this.setType(1);
		label.setText(config.getNam());
		label.setWidth(this.LABEL_WIDTH);
		textbox.setTitle(config.getNam().replace(":",""));
		textbox.setName(config.getDbn());
		textbox.setTabIndex(config.getTab());
		
		if(!config.getVal().isEmpty()){
			textbox.setValue(config.getVal());
		}
		
		if(!config.getSiz().equals("")){
			textbox.setWidth(config.getSiz().split(",")[0]+"px");
			if(config.getSiz().split(",").length > 1){
				textbox.setHeight(config.getSiz().split(",")[1]+"px");
			}
		}
		
		if(config.getEna() == 0) textbox.setEnabled(false);
		if(config.getVis() == 0){
			textbox.setVisible(false);
			label.setVisible(false);
		}
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hp.add(label);
		hp.add(textbox);
		
		if(!config.getEts().isEmpty()){
			setEvents(config.getEts());
		}
		
		textbox.addBlurHandler(new BlurHandler(){
			@Override
			public void onBlur(BlurEvent event) {
				Validator.getInstance().validateInput(TextBoxWrapper.this, textbox, config.getVld());				
			}
		});
		
		textbox.addFocusHandler(new FocusHandler(){
			@Override
			public void onFocus(FocusEvent event) {
				String styles = textbox.getStyleName();
				int indexOfInvalid = styles.indexOf("ui-Invalid");
				if(indexOfInvalid != -1){
					textbox.setValue("");
					textbox.removeStyleName("ui-Invalid");
				}
			}
		});
		
		initWidget(hp);
	}
	
	public void setEvents(String events){
		String eventList[] = events.split(",");
		
		for(int i=0; i<eventList.length; i++){
			final String eventType = eventList[i].split(":")[0];
			final String eventId = eventList[i].split(":")[1];
			 
			if(eventType.equals("change")){
				textbox.addValueChangeHandler(new ValueChangeHandler<String>(){
					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						EventHandler.getInstance().fireEvent(eventType, eventId);						
					}
				});
			} else if(eventType.equals("focus")){
				textbox.addFocusHandler(new FocusHandler(){
					@Override
					public void onFocus(FocusEvent event) {
						EventHandler.getInstance().fireEvent(eventType, eventId);
					}
				});
			} else if(eventType.equals("blur")){
				textbox.addBlurHandler(new BlurHandler(){
					@Override
					public void onBlur(BlurEvent event) {
						EventHandler.getInstance().fireEvent(eventType, eventId);
					}
				});
			} 
			
		}
		
		
		
	}

	public TextBox getTextbox() {
		return textbox;
	}

	public void setTextbox(TextBox textbox) {
		this.textbox = textbox;
	}

	public Label getLabel() {
		return label;
	}

	public void setLabel(Label label) {
		this.label = label;
	}
	
	@Override
	public void setValue(String value){ textbox.setText(value);}
	@Override
	public String getValue(){ return textbox.getText(); }
	@Override
	public void setEnabled(Boolean enabled) {textbox.setEnabled(enabled);}
	@Override
	public void setVisible(Boolean visible) {textbox.setVisible(visible); label.setVisible(visible);}
	@Override
	public void setTitle(String title) {textbox.setTitle(title);}
	@Override
	public String getTitle() {return textbox.getTitle();}
	@Override
	public String getText() {return label.getText();}
	@Override
	public void setText(String text) { label.setText(text); }
	@Override 
	public String getName(){ return textbox.getName(); }
	@Override
	public void setName(String name){ textbox.setName(name);}
	@Override
	public void setFocus(boolean focus){textbox.setFocus(focus);}
}
